﻿
Imports std
Imports runrroom

Public Class drserver
    Inherits rrserver

    Public Sub New()
        MyBase.New(connection_const.port.distrrun)
    End Sub

    Protected Overrides Function return_jobid() As Boolean
        Return True
    End Function

    Protected Overrides Function run(ByRef id As String, ByVal cmd As vector(Of String), _
                                     ByVal distrRuleId As String, ByVal rule As String, _
                                     ByRef errorInfo As String) As Boolean
        Dim r As distrrule = Nothing
        If Not isemptystring(rule) Then
            If Not distrrule.parse(rule, r) Then
                errorInfo = "cannot parse distrRule " + rule
                Return False
            End If
        ElseIf Not isemptystring(distrRuleId) Then
            If Not distrrule_map().find(distrRuleId, r) Then
                errorInfo = "cannot find distrRuleId " + distrRuleId
                Return False
            End If
        Else
            errorInfo = "no valid distrRuleId or distrRule sent to drserver."
            Return False
        End If

        If Not jobmgr().addjob(cmd, r, id) Then
            errorInfo = "cannot add job to jobmgr, id " + id + ", cmd " + Convert.ToString(cmd) + _
                        ", rule " + Convert.ToString(r)
            Return False
        Else
            Return True
        End If
    End Function

    Protected Overrides Function status(ByVal id As String, ByRef percentage As Int64, _
                                        ByRef errorInfo As String) As Boolean
        If isemptystring(id) Then
            errorInfo = "no valid jobid sent to rrserver."
            Return False
        End If
        Dim p As percentage_t = Nothing
        If Not jobmgr().percent(id, p) Then
            errorInfo = "cannot find jobid " + id + " in jobmgr."
            Return False
        End If
        percentage = (+p)
        Return True
    End Function
End Class
